import 'package:xws/helpers/as_t.dart';
import 'package:xws/models/goods/comment.dart';
import 'package:xws/models/goods/comment_obj.dart';
import 'package:xws/models/goods/goods_obj.dart';
import 'package:xws/models/goods/pub_obj.dart';
import 'package:xws/models/goods/reply_obj.dart';
import 'package:xws/models/res_obj.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request_apis.dart';
import 'package:xws/utils/dio_utils/request_source.dart';
import 'package:xws/utils/log_util.dart';

/// 物品类型
enum GoodsSearchType { LOCATION, FRIEND, OWNER, GET, FAVORITE }

/// 类型值
const List<String> _types = <String>['LOCATION', 'FRIEND', 'OWNER', 'GET', 'FAVORITE'];

extension _ExT on GoodsSearchType {
  String get str => _types[index];
}

/// 发布物品
Future<bool> publishGoods(PubObj pubObj) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.pubGoods,
      method: 'post',
      data: pubObj.toJson(),
      contentType: 'application/json',
    );

    if (res == null || !res.success) {
      ct(res?.message ?? '网络错误');
      return false;
    }

    st('发布成功');

    return res.success;
  } catch (e) {
    error('发布物品出错:$e');

    return false;
  }
}

/// 编辑并保存物品
Future<bool> editGoods(Map<String, dynamic> data) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.editGoods,
      method: 'put',
      data: data,
      contentType: 'application/json',
    );

    if (res == null || !res.success) {
      ct(res?.message ?? '网络错误');
      return false;
    }

    st('保存成功');

    return res.success;
  } catch (e) {
    error('编辑并保存物品出错:$e');

    return false;
  }
}

/// 获取物品列表
Future<List<GoodsObj>> getGoodsList({
  String? city,
  String? keyword,
  GoodsSearchType? searchType,
  List<String>? goodsIdList,
  double? latitude,
  double? longitude,
  bool? cityCondition,
  bool? fiveKmCondition,
  String? userId,
  int pageNum = 1,
  int pageSize = 10,
}) async {
  try {
    return await getList<GoodsObj>(
      APIS.goods,
      dataKey: 'records',
      contentType: 'application/json',
      data: <String, dynamic>{
        if (city != null) 'city': city,
        if (keyword != null) 'keyword': keyword,
        if (searchType != null) 'searchType': searchType.str,
        if (goodsIdList != null) 'goodsIdList': goodsIdList,
        if (latitude != null) 'latitude': latitude,
        if (longitude != null) 'longitude': longitude,
        if (cityCondition != null) 'cityCondition': cityCondition,
        if (fiveKmCondition != null) 'distance': 5,
        if (userId != null) 'userId': userId,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
  } catch (e) {
    error('获取物品列表出错:$e');

    return <GoodsObj>[];
  }
}

/// 点赞物品
Future<bool> likeGoods(String id) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.likeGoods,
      method: 'put',
      data: <String, dynamic>{'goodsId': id},
      contentType: 'application/x-www-form-urlencoded',
    );

    if (res == null || !res.success) {
      return false;
    }

    return res.success;
  } catch (e) {
    error('点赞物品出错:$e');
    return false;
  }
}

/// 评论物品
Future<T?> commentGoods<T extends Comments>({
  String? goodsId,
  String? commentId,
  String? content,
}) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.commentGoods,
      method: 'put',
      data: <String, dynamic>{
        'goodsId': goodsId,
        if (commentId != null) 'commentId': commentId,
        'content': content,
      },
      contentType: 'application/json',
    );

    if (res == null || !res.success) {
      et(res?.message ?? '网络错误');
      return null;
    }

    if (T == CommentObj) return CommentObj.fromJson(asSDMap(res.data)) as T;
    if (T == ReplyObj) return ReplyObj.fromJson(asSDMap(res.data)) as T;

    return null;
  } catch (e) {
    error('评论物品出错:$e');
    return null;
  }
}

/// 收藏物品
/// * `isCollect` 物品原来的收藏状态
Future<bool> collectGoods(String id, bool isCollect) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.collectGoods,
      method: 'put',
      data: <String, dynamic>{'goodsId': id},
      contentType: 'application/x-www-form-urlencoded',
    );

    if (res == null || !res.success) {
      return false;
    }

    isCollect ? us.delACollect() : us.addACollect();

    return res.success;
  } catch (e) {
    error('收藏物品出错:$e');
    return false;
  }
}

/// 删除物品
Future<bool> delGoods(String id) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.delGoods,
      method: 'delete',
      data: <String, dynamic>{'goodsId': id},
      contentType: 'application/x-www-form-urlencoded',
    );

    if (res == null || !res.success) {
      return false;
    }

    return res.success;
  } catch (e) {
    error('删除物品出错:$e');
    return false;
  }
}

/// 分享物品
Future<bool> shareAGoods(String goodsId) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.shareGoods,
      method: 'put',
      data: <String, dynamic>{'goodsId': goodsId},
      contentType: 'application/x-www-form-urlencoded',
    );

    if (res == null || !res.success) {
      return false;
    }

    return res.success;
  } catch (e) {
    error('分享物品出错:$e');
    return false;
  }
}

/// 赠送物品
Future<bool> giveGoods(String goodsId, String userId) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.giveGoods,
      method: 'put',
      data: <String, dynamic>{'goodsId': goodsId, 'userId': userId},
      contentType: 'application/x-www-form-urlencoded',
    );

    if (res == null || !res.success) {
      return false;
    }

    return res.success;
  } catch (e) {
    error('赠送物品出错:$e');
    return false;
  }
}
